package com.sinooceangroup.voiceassistant;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.LogRecord;

class VoiceAssistantAdapter extends BaseAdapter {

    private List<VoiceModel> listData;
    private LayoutInflater mInflater;

    private Context context;

    public VoiceAssistantAdapter(List<VoiceModel> listData, Context context) {
        this.listData = listData;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public VoiceModel getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(R.layout.adapter_voice_assistant_item, parent, false); //加载布局

        TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
        TextView tv2 = (TextView) convertView.findViewById(R.id.tv2);
        VoiceModel item = getItem(position);
        if (item.isUser()) {//避免因为view复用导致的数据混乱
            tv1.setText("");
            tv2.setText(item.getMessage());
        } else {
            tv2.setText("");
            tv1.setText(item.getMessage());
        }
        convertView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                Log.i("VoiceAssistantActivity", "listView的总高度:listItem.position:" + position + " bottom-top " + (bottom - top));
                heightMap.put(position, bottom - top);
            }
        });
        return convertView;
    }

    private Map<Integer, Integer> heightMap = new HashMap<>();

    public int getItemHeight(int position) {
        return heightMap.containsKey(position) ? heightMap.get(position) : 0  ;
    }

    public int getListViewHeight() {
        int total = 0;
        for (int key : heightMap.keySet()) {
            total += key;
        }
        return total;
    }

}
